//
//  GXOrderNewsViewController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/27.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXOrderNewsViewController.h"
#import "GXOrderNewsViewCell.h"
#import "GXOrderNewsModel.h"
@interface GXOrderNewsViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _newsTableview;
}
@property (nonatomic, strong) UIView * totalView;
@property (nonatomic, strong) UIButton * rightBtn;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIButton * deleteBtn;
@property (nonatomic, strong) NSMutableArray *deleteArr;//删除数据的数组
@property (nonatomic, assign) BOOL isEdit;//编辑状态
@property (nonatomic, assign) BOOL isSelect;//选中

@end

@implementation GXOrderNewsViewController
static NSString *const orderNewsViewCell=@"GXOrderNewsViewCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单消息";
    self.isEdit = false;
    self.isSelect = false;
    self.deleteArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self initTableView];
    [self setUpNavigationRightBarButtonItem];
}

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
        NSString * filePath = [[NSBundle mainBundle]pathForResource:@"newsDataSource" ofType:@"plist"];
        NSMutableArray * array =[[NSMutableArray alloc] initWithContentsOfFile:filePath];
        for (NSDictionary * dict in array) {
            GXOrderNewsModel * model = [GXOrderNewsModel mj_objectWithKeyValues:dict];
            [_dataArray addObject:model];
        }
    }
    return _dataArray;
}
-(void)setUpNavigationRightBarButtonItem
{
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame=CGRectMake(0,5,40,20);
    rightBtn.backgroundColor = [UIColor redColor];
    [rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [rightBtn setTitle:@"取消" forState:UIControlStateSelected];
    [rightBtn setTintColor:[UIColor whiteColor]];
    _rightBtn = rightBtn;
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [rightBtn addTarget:self action:@selector(enterRightButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtn=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem=rightBarBtn;
}
#pragma mark -- 创建uitableview
-(void)initTableView{
    UITableView *homeTable=[[UITableView alloc] initWithFrame:CGRectMake(10,0,kUIScreenWidth-20,kUIScreenHeight-Height_TabBar-Height_StatusBar-40) style:UITableViewStyleGrouped];
    homeTable.delegate=self;
    homeTable.dataSource=self;
    homeTable.sectionHeaderHeight = 20;
    homeTable.sectionFooterHeight = 0.01;
    homeTable.showsHorizontalScrollIndicator=NO;
    homeTable.showsVerticalScrollIndicator=NO;
    homeTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    homeTable.backgroundColor = kGlobalbgColor;
    _newsTableview=homeTable;
    [_newsTableview registerNib:[UINib nibWithNibName:@"GXOrderNewsViewCell" bundle:nil] forCellReuseIdentifier:orderNewsViewCell];
    [self.view addSubview:homeTable];
}
-(void)setUpTableHeaderView
{
    UIView * totalView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 30)];
    totalView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _totalView = totalView;
    _newsTableview.tableHeaderView = totalView;
    UIButton *selectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    selectBtn.frame=CGRectMake(0,5,70,30);
    [selectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 7, 0, 15)];
    [selectBtn setTitle:@"全选" forState:UIControlStateNormal];
//    [selectBtn setTitle:@"取消全选" forState:UIControlStateSelected];
    [selectBtn setImage:[UIImage imageNamed:@"common_uncheck"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"common_checked"] forState:UIControlStateSelected];
    [selectBtn addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [selectBtn setTitleColor:RGBColor(109, 213, 200) forState:UIControlStateNormal];
    selectBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [totalView addSubview:selectBtn];
}
-(void)enterRightButton:(UIButton *)btn
{
    btn.selected=!btn.selected;
    if (btn.selected) {
        if (_dataArray.count) {
            [self setUpTableHeaderView];
            [self creatDeleteButton];
            self.isEdit = YES;
        }
    }else{
        [_totalView removeFromSuperview];
        [self.deleteBtn removeFromSuperview];
        _newsTableview.tableHeaderView.height= 0.01;
        self.isEdit = NO;
    }
    [_newsTableview reloadData];
}
-(void)creatDeleteButton
{
    UIButton * deleteAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    deleteAllBtn.frame = CGRectMake(0, kUIScreenHeight-Height_TabBar-40-Height_StatusBar, kUIScreenWidth,40);
    [deleteAllBtn setTitle:@"删除" forState:UIControlStateNormal];
    [deleteAllBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.deleteBtn = deleteAllBtn;
    deleteAllBtn.backgroundColor = [UIColor redColor];
    deleteAllBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [deleteAllBtn addTarget:self action:@selector(pressDeleteAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:deleteAllBtn];
}
//删除
- (void)pressDeleteAllBtnClick:(UIButton *)btn
{
        //删除
        [self.dataArray removeObjectsInArray:self.deleteArr];
        _totalView.frame = CGRectMake(0, 0, 0, CGFLOAT_MIN);
        [_totalView removeFromSuperview];
        [self.deleteBtn removeFromSuperview];
        _rightBtn.selected = normal;
        [_newsTableview reloadData];
}
//全选
-(void)selectAllBtnClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
//    if (btn.selected) {
    self.isSelect = btn.selected;
//        for (int i = 0; i <self.dataArray.count; i ++) {
//            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:i];
//            GXOrderNewsModel * model = self.dataArray[indexPath.section];
//            model.buttonSate = YES;
//            [_newsTableview selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
//            [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.section]];
//        }
        
    
    [_newsTableview reloadData];
}
#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GXOrderNewsViewCell * orderCell = [tableView dequeueReusableCellWithIdentifier:orderNewsViewCell];
    if (self.dataArray[indexPath.section]) {
        GXOrderNewsModel * model = self.dataArray[indexPath.section];
//        if (self.isEdit) {
//            model.isEdit = YES;
//        }else {
//            model.isEdit = false;
//        }
        model.isEdit = self.isEdit;
        if (self.isSelect) {
              model.buttonSate = self.isSelect;
        }
        orderCell.model = model;
        orderCell.selectionStyle =  UITableViewCellSelectionStyleNone;
        orderCell.selectButtonBlock = ^(UIButton * _Nonnull selectButton) {
            
//            model.buttonSate = selectButton.selected;
            if (selectButton.selected) {
                model.buttonSate = false;
                [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.section]];
                 MyLog(@"self.deleteArr--%@",self.deleteArr);
            }else
            {
                 model.buttonSate = false;
                 self.isSelect = false;
                 [self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.section]];
                 MyLog(@"1111self.deleteArr--%@",self.deleteArr);
            }
//            NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:indexPath.section];
//            [tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//            [_newsTableview reloadData];
        };
    }
    return orderCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}


@end
